To enable CORS
In your web api project
Install-Package Microsoft.AspNet.WebApi.Cors
In the WebApiConfig,Register method
config.EnableCors();
In the web api controller class
sing System.Web.Http.Cors;
namespace WebService.Controllers
{
[EnableCors(origins: "http://mywebclient.azurewebsites.net", headers: "*", methods: "*")]
public class TestController : ApiController
{
Attribute based routing.
Note: You need to decorate with the post or get for it to work properly.
Set this up in WebApiConfig.cs as following
To get the latest bits (if running VS 2012)
Install-Package Microsoft.AspNet.WebApi.WebHost
Ya gotta love this
this goes in global.asax
GlobalConfiguration.Configure(WebApiConfig.Register);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
GlobalConfiguration.Configuration.EnsureInitialized();
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Then you can decorate the controller methods as follows
[Route("customers/{customerId}/orders")]
[HttpGet]
public string GetOrdersByCustomer(int customerId)
{
return "";
}
This would support the following url:
http://localhost:45910/customers/1/orders
To set the web api to return json as a default (this is in WebApi.config)
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
THIS INFO IS POTENTIALLY MADE OBSOLETE BY THE WEB API INFO ABOVE
the web api will limit you to a single post and get unless
1. You remove the default get
2. Decorate the subsequent posts and gets with an action name
3. Add code to the routing to allow for a 5 section url'
routes.MapHttpRoute(
name: "API Default multiple",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
eg.
[HttpGet]
[ActionName("CheckChargeCodes")]
public string Get(string id)
This will enable a get like:
/api/ServiceRequest/CheckChargeCodes/H20000